# 该文件为更改xml的名称和xml中label的值，并将对应image的名称也进行更改，然后存到一个新的文件夹中
import xml.etree.ElementTree as ET
import os
import shutil


xml_num = 1000


def FindxmlAndChangelabel(in_path,out_path):
    tree = ET.parse(in_path)
    root = tree.getroot()
    for one_obeject in root.iter('object'):
        # 这里也可以用root.find()[只发现第一个], root.findall()[发现所有的]
        one_obeject.find('name').text = '07'
    tree.write(out_path)
    os.remove(in_path)
# 测试函数FindxmlAndChangelabel('/Users/jiangce/Desktop/pycharm/Company/01_1.xml','/Users/jiangce/Desktop/pycharm/Company/output.xml')


# 改变xml
if __name__ == '__main__':
    ini_file_path = 'C:\\Users\\jiangce\\Desktop\\车型识别2\\VOCdevkit\\xml2'
    change_file_path = 'C:\\Users\\jiangce\\Desktop\\敞篷货车'
    out_new_image_path = 'C:\\Users\\jiangce\\Desktop\\车型识别2\\VOCdevkit\\img2'
    images_list = os.listdir(change_file_path)
    # 存储改为敞篷货车的名称
    images_name = []
    # 将敞篷货车的名字存入images_name
    for image in images_list:
        image_name = image.split('.')[0]
        images_name.append(image_name)
    for name in images_name:
        xml_name = name + '.xml'
        in_path_xml = os.path.join(ini_file_path,xml_name)
        xml_num += 1
        out_xml_name = '07_' + str(xml_num) + '.xml'
        out_path_xml = os.path.join(ini_file_path,out_xml_name)
        FindxmlAndChangelabel(in_path_xml,out_path_xml)
        new_image_name = '07_' + str(xml_num) + '.jpg'
        new_image_path = os.path.join(out_new_image_path,new_image_name)
        ini_image_name = name + '.jpg'
        ini_image_path = os.path.join(change_file_path,ini_image_name)
        shutil.copy(ini_image_path,new_image_path)